//
//  DGHLaunchImageViewController.m
//  czy_daishugj_store_ios
//
//  Created by czy001 on 17/1/22.
//  Copyright © 2017年 daishugj. All rights reserved.
//

#import "DGHLaunchImageViewController.h"
#import "SDWebImageManager.h"

@interface DGHLaunchImageViewController ()
{
    NSTimer *timer;
    UIImageView *mainImg;
    UIButton *skipBtn;
}
@end

@implementation DGHLaunchImageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSDictionary *adDic=[[NSUserDefaults standardUserDefaults] objectForKey:@"ADSIMAGE"];
    mainImg=[[UIImageView alloc] initWithFrame:self.view.frame];
    mainImg.userInteractionEnabled=YES;
    [self.view addSubview:mainImg];
    
    NSString *url=[NSString stringWithFormat:@"%@-%.0f|%.0f",JSON(adDic[@"picUrl"]),SCREENWIDTH*2,SCREENHEIGHT*2];
    NSString *str1 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    BOOL isExit = [[SDWebImageManager sharedManager] diskImageExistsForURL:[NSURL URLWithString:str1]];
    
    if (adDic) {
        if (!isExit) {
            _totalTime=0;
        }
        else
        {
            [mainImg sd_setImageWithURL:[NSURL URLWithString:str1]];
            _totalTime=4;
            picUrl=JSON(adDic[@"linkUrl"]);
            
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(adPicHasTouch:)];
            tap.numberOfTapsRequired=1;
            [mainImg addGestureRecognizer:tap];
        }
    }
    else
    {
        _totalTime=0;
    }

    
    if (_totalTime>0) {
        skipBtn=[UIButton buttonWithType:UIButtonTypeCustom];
        skipBtn.frame=CGRectMake(SCREENWIDTH-80, 40, 50, 30);
        [skipBtn setTitle:[NSString stringWithFormat:@"%is",_totalTime] forState:UIControlStateNormal];
        [skipBtn setBackgroundColor:[UIColor blackColor]];
        skipBtn.alpha=0.8;
        skipBtn.layer.cornerRadius=5;
        skipBtn.layer.masksToBounds=YES;
        [skipBtn addTarget:self action:@selector(exchangeVC) forControlEvents:UIControlEventTouchUpInside];
        [mainImg addSubview:skipBtn];
    }
    
    
    timer=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeSkipTitle) userInfo:nil repeats:YES];
    
    NSDictionary *launchDic=[[NSUserDefaults standardUserDefaults] objectForKey:@"LAUNCHIMAGE"];
    int scale=2;
    if (SCREENWIDTH == 414) {
        scale=3;
    }
    
    _launchView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width , [[UIScreen mainScreen] bounds].size.height)];
    if (launchDic) {
        NSString *url=[NSString stringWithFormat:@"%@-%.0f|%.0f",JSON(launchDic[@"picUrl"]),SCREENWIDTH*scale,SCREENHEIGHT*scale];
        NSString *str1 = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [_launchView sd_setImageWithURL:[NSURL URLWithString:str1]];
        
        [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(removeLunch) userInfo:nil repeats:NO];
    }
    else
    {
        if (adDic) {
            [self removeLunch];
        }
        else
        {
            [self exchangeVC];
        }
    }
    
    _launchView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:_launchView];
    
}

//广告调转
- (void)adPicHasTouch:(UITapGestureRecognizer *)tap
{
    if (picUrl.length>0) {
    
        [[NSUserDefaults standardUserDefaults] setObject:picUrl forKey:@"ADURL"];
        [self exchangeVC];
    }
}

-(void)removeLunch
{
    [UIView animateWithDuration:0.3 animations:^{
        self.launchView.alpha=0;
    } completion:^(BOOL finished) {
        [self.launchView removeFromSuperview];
        self.launchView = nil;
    }];
}

- (void)changeSkipTitle
{
    _totalTime--;
    
    [skipBtn setTitle:[NSString stringWithFormat:@"%is",_totalTime] forState:UIControlStateNormal];
    skipBtn.titleLabel.text=[NSString stringWithFormat:@"%is",_totalTime];
    
    if (_totalTime<1) {
        [self exchangeVC];
    }

}

- (void)skipToNext
{
    [self exchangeVC];
}

//切换视图
- (void)exchangeVC
{
    [timer invalidate];
    //发送自动登陆状态通知
    [[NSNotificationCenter defaultCenter] postNotificationName:KNOTIFICATION_LOGINCHANGE object:@([[EMClient sharedClient] isLoggedIn])];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
